package com.xlf.book.dao.impl;

import com.xlf.book.dao.BookTypeDao;
import com.xlf.book.po.BookType;
import com.xlf.book.util.DButil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xie'lin'feng
 * @date 2020-07-17-17:08
 * @description 图书类型数据接口实现类
 */
public class BookTypeDaoImpl implements BookTypeDao {

    @Override
    public List<BookType> queryAll() {
        List<BookType> list = new ArrayList<>();
        Connection conn = DButil.open();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from booktype";
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            list = (List<BookType>) DButil.infoRs(rs,BookType.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(rs,ps,conn);
        }
        return list;
    }

    @Override
    public BookType queryById(Integer typeId) {
        BookType bookType = null;
        Connection conn = DButil.open();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from booktype where typeId = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,typeId);
            rs = ps.executeQuery();
            if (rs.next()){
                bookType = new BookType(rs.getInt("typeId"),rs.getString("typeName"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(rs,ps,conn);
        }
        return bookType;
    }
}
